﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Security;
using System.Text;
using System.Xml;
using Unknown.Framework.Develop;

namespace Unknown.Framework.Storage
{
    /// <summary>
    /// XML文件处理类
    /// </summary>
    public static class XmlFileHandler
    {
        /// <summary>
        /// 读
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>内容</returns>
        public static string Read(string file)
        {
            string result = null;

            XmlDocument xmlDocument = null;
            try
            {
                xmlDocument = new XmlDocument();
                xmlDocument.Load(file);
                result = xmlDocument.InnerXml;
            }
            catch (ArgumentNullException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (ArgumentException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (DirectoryNotFoundException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (FileNotFoundException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (PathTooLongException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (IOException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (NotSupportedException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (SecurityException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (UnauthorizedAccessException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (XmlException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }

            return result;
        }

        /// <summary>
        /// 写
        /// </summary>
        /// <param name="file">文件</param>
        /// <param name="content">内容</param>
        public static void Write(string file, string content)
        {
            MemoryStream memoryStream = null;

            try
            {
                byte[] buffer = System.Text.UTF8Encoding.UTF8.GetBytes(content);

                memoryStream = new MemoryStream();
                memoryStream.Write(buffer, 0, buffer.Length);
                memoryStream.Seek(0, SeekOrigin.Begin);

                XmlReader xmlReader = XmlReader.Create(memoryStream);

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlReader);

                FileInfo fileInfo = new FileInfo(file);
                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }

                xmlDocument.Save(file);
            }
            catch (ArgumentNullException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (EncoderFallbackException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (ArgumentException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (DirectoryNotFoundException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (PathTooLongException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (IOException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (NotSupportedException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (ObjectDisposedException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (SecurityException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (UnauthorizedAccessException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            catch (XmlException exception)
            {
                Collection<string> remarks = new Collection<string>();
                remarks.Add(file);

                CrashHandlers.Default.Hibernate(exception, remarks);
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Close();
                }
            }
        }
    }
}
